package com.wtwd.campus.receiver;

import com.alibaba.fastjson.JSONObject;
import com.wtwd.campus.annotation.MsgType;
import com.wtwd.campus.common.Constants;
import com.wtwd.campus.entity.DeviceInfo;
import com.wtwd.campus.entity.MqDeviceImeid;
import com.wtwd.campus.entity.MqMessage;
import com.wtwd.campus.h5_manager.entity.DeviceLog;
import com.wtwd.campus.h5_manager.service.DeviceLogService;
import com.wtwd.campus.service.DeviceInfoService;
import com.wtwd.campus.utils.ReceiverUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
@MsgType(value = Constants.DEVICE_IMEID)
public class DeviceImeidReceiverHandler implements IReceiverHandler {
    @Autowired
    private DeviceInfoService deviceInfoService;
    @Autowired
    private ReceiverUtils receiverUtils;
    @Autowired
    private DeviceLogService deviceLogService;

    @Override
    public void handlerMessage(MqMessage mqMessage) {
        MqDeviceImeid mqDeviceImeid = (MqDeviceImeid) mqMessage.getObject();
        String imei = mqDeviceImeid.getImei();
        String commandNumber = mqDeviceImeid.getCommandNumber();
        //处理指令回复的时间
        String deviceDate = mqDeviceImeid.getDeviceDate();
        String deviceTime = mqDeviceImeid.getDeviceTime();
        Long time = getTime(deviceDate, deviceTime);
        DeviceInfo deviceInfo = deviceInfoService.getDeviceByImei(imei);
        if (deviceInfo != null) {
            //修改设备信息 2021年7月19日16:51:43 无须修改imei
//            if (!StringUtils.isBlank(mqDeviceImeid.getUploadImeiId())) {
//                deviceInfo.setImei(mqDeviceImeid.getUploadImeiId());//修改设备imei号
//            }
            deviceInfo.setLastUpTime((new Date()).getTime());
            deviceInfoService.modifyDeviceInfo(deviceInfo);
            //删除缓存中的指令
            String delKey = Constants.DEVICE_IMEID.concat("-").concat(commandNumber);
            receiverUtils.deleteCommandInRedis(imei, delKey);
            //完善指令下发记录
            receiverUtils.completeCommandRecord(Constants.DEVICE_IMEID, imei, commandNumber, null, Constants.COMMAND_COMPLETE, time);
            //存储设备接收日志
            DeviceLog deviceLog = new DeviceLog();
            deviceLog.setCommandNumber(commandNumber);
            deviceLog.setSendUser(Constants.DEVICE_USER);
            deviceLog.setImei(imei);
            deviceLog.setBigType(Constants.MESS_TYPE);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("设备号", imei);
            jsonObject.put("设备上报的imeid", mqDeviceImeid.getUploadImeiId());
            jsonObject.put("经度", mqDeviceImeid.getMqLocationData().getLongitude());
            jsonObject.put("纬度", mqDeviceImeid.getMqLocationData().getLongitude());
            jsonObject.put("设备时间", deviceTime);
            jsonObject.put("日期", mqDeviceImeid.getDeviceDate());
            jsonObject.put("设备状态", mqDeviceImeid.getDeviceStatus());
            deviceLog.setCmd(jsonObject.toJSONString());
            deviceLog.setSendTime(System.currentTimeMillis());
            deviceLog.setState(1);
            deviceLogService.addDeviceLog(deviceLog);
        } else {
            saveUnInStorage(imei);
            receiverUtils.cloaseChannel(imei);
        }
    }
}
